exports.sendErr = function (err= "server internal error", errCode = 500){
    return{
        code:errCode,
        msg:err,
        data: null
    }
}


exports.sendResult = function (result){
    return{
        code:0,
        msg:"",
        data:result
    }
}
exports.asyncHandler = (handler) =>{
    return async (req,res,next) => {
            const result = await handler(req,res,next)
            
            if(result.code === 401){
                res.send(exports.sendErr(result.err,result.code))
            }else if(result instanceof Error){
                next(new Error(result))
                return
            }
            res.send(exports.sendResult(result))
    }
    
}